﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartParking.Entities;
using SmartParking.IService;
using SmartParking.Models;
using SmartParking.Models.EventAggregatorModel;
using SmartParking.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace SmartParking.ViewModels.Pages
{
    public class MenuManagementViewModel : ViewModelBase<MenuItemModel>
    {
        IMenuService _menuService;
        IDialogService _dialogService;
        IEventAggregator _eventAggregator;

        private List<SysMenu> _originMenu = [];

        public MenuManagementViewModel(IRegionManager regionManager,
            IMenuService menuService,
            IDialogService dialogService,
            IEventAggregator eventAggregator) : base(regionManager)
        {
            PageTitle = "菜单管理";

            _menuService = menuService;
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;

            Refresh();
        }

        public ObservableCollection<MenuItemModel> Menus { get; set; } = [];

        public override void Refresh()
        {
            Menus.Clear();
            _originMenu = _menuService.GetMenuList(SearchKey).ToList();
            FillMenus(Menus, 0);
        }

        private void FillMenus(ObservableCollection<MenuItemModel> models, int parentId)
        {
            foreach (var item in _originMenu.Where(t => t.ParentId == parentId))
            {
                MenuItemModel model = new MenuItemModel
                {
                    MenuId = item.Id,
                    MenuHeader = item.MenuHeader ?? "",
                    MenuIcon = item.MenuIcon ?? "",
                    TargetView = item.TargetView ?? "",
                    ParentId = parentId,
                    MenuType = item.MenuType ?? 1,
                    IsExpanded = true,
                };
                models.Add(model);

                FillMenus(model.Children, item.Id);
            }

            if (parentId > 0 && models.Count > 0)
            {
                models[models.Count - 1].IsLastChild = true;
            }
        }

        public override void DoModify(MenuItemModel model)
        {
            DialogParameters dp = new DialogParameters();
            dp.Add("model", model);
            _dialogService.ShowDialog("ModifyMenuView", dp, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
                    this.Refresh();
                }
            
            });
        }

        public override void DoDelete(MenuItemModel item)
        {
            try
            {
                if (MessageBox.Show("确定要删除菜单项吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    return;
                }

                if (_menuService.Any<SysMenu>(t=>t.ParentId == item.MenuId))
                {
                    throw new Exception("请先删除子菜单项！");
                }

                _menuService.Delete<SysMenu>(item.MenuId);

                // 逻辑删除
                // 通过特定字段进行数据过滤
                MessageBox.Show("删除完成！", "提示");

                this.Refresh();

                _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "发生错误！");
            }
        }
    }
}
